﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using My;

public partial class AttendanceWebForm_LeaveData_Form : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void LoadData(FormViewMode mode, DataTable dt)
    {
        fv.ChangeMode(mode);

        switch (mode)
        {
            case FormViewMode.Insert:
                break;
            case FormViewMode.Edit:
                break;
            case FormViewMode.ReadOnly:
                break;
        }
    }

    protected void fv_DataBound(object sender, EventArgs e)
    {
        switch (fv.CurrentMode)
        {
            case FormViewMode.Insert:
                (fv.FindControl("txtEmpID") as ITextControl).Text = EmpManager.Current.EmpID;
                break;
            case FormViewMode.Edit:
                break;
            case FormViewMode.ReadOnly:
                break;
        }
    }

    protected void fv_ItemCommand(object sender, FormViewCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "MyCancel":
                Canceled(this, EventArgs.Empty);
                break;
        }
    }

    protected void fv_Saving(object sender, MyFormViewSavingEventArgs e)
    {
        if (Page.IsValid == false)
        {
            e.IsValid = false;
            return;
        }

        var db = new DB("Attendance");

        var paras = new List<DBParameter>();
        var query = string.Empty;

        if (fv.CurrentMode == FormViewMode.Insert)
        {
            query = string.Format(@"INSERT INTO [dbo].[LeaveData]
           ([LeaveType]
           ,[OrgID]
           ,[EmpID]
           ,[Status]
           ,[LeaveStartDate]
           ,[LeaveEndDate]
           ,Hours
           ,[CreateBy]
           ,[CreateDate])

           SELECT @LeaveType, e.OrgID, @EmpID, @Status, @LeaveStartDate, @LeaveEndDate, @Hours, @CreateBy, getdate()
            FROM EmpData e WHERE e.EmpID = @EmpID ");

            paras.Add(new DBParameter("CreateBy", EmpManager.Current.EmpID));
            paras.Add(new DBParameter("Status", "1"));
        }

        paras.Add(new DBParameter("EmpID", (fv.FindControl("txtEmpID") as ITextControl).Text));
        paras.Add(new DBParameter("LeaveType", (fv.FindControl("ddlLeaveType") as ITextControl).Text));
        paras.Add(new DBParameter("LeaveStartDate", (fv.FindControl("txtLeaveStartDate") as IValueControl<DateTime?>).Value + (fv.FindControl("txtLeaveStartTime") as IValueControl<TimeSpan>).Value));
        paras.Add(new DBParameter("LeaveEndDate", (fv.FindControl("txtLeaveEndDate") as IValueControl<DateTime?>).Value + (fv.FindControl("txtLeaveEndTime") as IValueControl<TimeSpan>).Value));
        paras.Add(new DBParameter("Hours", this.LeaveHours));

        db.ExecuteNonQuery(query, paras);
    }

    protected void fv_Saved(object sender, EventArgs e)
    {
        this.LoadData(FormViewMode.Insert, null);
        Saved(this, EventArgs.Empty);
    }

    public event EventHandler Saved;
    public event EventHandler Canceled;

    protected float LeaveHours { get; set; }
}